package com.wyy.springcloud.controller;

import com.wyy.springcloud.pojo.Dept;
import com.wyy.springcloud.sevice.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.net.URI;
import java.util.List;

@RestController
public class DeptController {

    @Autowired
    private DeptService deptService;

    @Autowired
    private DiscoveryClient discoveryClient;

    @PostMapping("/dept/add")
    public boolean addDept(@RequestBody Dept dept){
        return deptService.addDept(dept);
    }

    @GetMapping("/dept/get/all")
    public List<Dept> queryDeptAll(){

        return deptService.queryDeptAll();
    }
    @GetMapping("/dept/get/{id}")
    public Dept queryDeptById(@PathVariable("id") Long deptno){
        return deptService.queryDeptById(deptno);
    }

    @GetMapping("/dept/discovery")
    public Object discoveryClient(){
        //获取微服务列表清单
        List<String> services = discoveryClient.getServices();
        System.out.println(services);
        List<ServiceInstance> instances = discoveryClient.getInstances("SPRINGCLOUD-PROVIDER-DEPT");
        for (ServiceInstance instance : instances) {

            String host = instance.getHost();
            int port = instance.getPort();
            URI uri = instance.getUri();
            String serviceId = instance.getServiceId();
            System.out.println(host+"\t"+port+"\t"+uri+"\t"+serviceId);
        }
        return this.discoveryClient;
    }
}
